<?php

/**
 * 优惠信息
 * @author Ice <iceinto@mallog.com.cn>
 */
class MallDiscountModel extends BaseModel
{
    public $_type = 'discount';

    /**
     * 获取对应城市热卖
     * @param int $city
     * @param int $mid
     * @return array|mixed
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getCityMallDis($city = null, $mid = null, $limit = null)
    {
        if (!$city) {
            $city = $this->getCity();
        }
        $data = array();
        $map = array();
        if ($mid) {
            $map['m_market.id'] = $mid;
        }
        $map['m_market.city'] = $city;
        $map['m_mall_discount.end_time'] = array('egt', $this->getThisDayTime());
        $data = $this->join('INNER JOIN m_market ON m_mall_discount.mid = m_market.id')
            ->join('INNER JOIN m_brand ON m_mall_discount.bid = m_brand.id')
            ->field('m_mall_discount.itemid,
            m_mall_discount.title,
            m_mall_discount.image,
            m_mall_discount.k1,
            m_mall_discount.k2,
            m_market.name as mall_name,
            m_brand.name as brand_name,
            m_brand.png as brand_png,
            m_brand.small_image as brand_image
            ')->limit($limit)
            ->where($map)
            ->order('m_mall_discount.start_time DESC')
            ->select();

        $this->setImage($data);
        return $data;
    }

    public function getCityMallDisLine($city = null, $mid = null, $limit = null)
    {
        if (!$city) {
            $city = $this->getCity();
        }
        $data = array();
        $map = array();
        if ($mid) {
            $map['m_market.id'] = $mid;
        }
        $map['m_market.city'] = $city;
        $map['m_mall_discount.end_time'] = array('egt', $this->getThisDayTime());
        $data = $this->join('INNER JOIN m_market ON m_mall_discount.mid = m_market.id')
            ->join('INNER JOIN m_brand ON m_mall_discount.bid = m_brand.id')
            ->field('m_mall_discount.itemid,m_mall_discount.title,m_brand.name as brand_name
            ')->limit($limit)
            ->where($map)
            ->order('m_mall_discount.start_time DESC')
            ->select();
        return $data;
    }

    /**
     * 设置图片信息
     * @param array $ads 列表数组
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function setImage(&$data)
    {
        if ($data) {
            foreach ($data as $key => $item) {
                $data[$key]['image_url'] = $this->getImageUrl($item['image']);
                if ($item['brand_png']) {
                    $data[$key]['png_url'] = D('Brand')->getBrandImage($item['brand_png']);
                }
            }
        }
    }

    /**
     * 获取热门特卖
     * @return mixed
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getHot()
    {
        $data = $this->getCityMallDis(null, null, '0,6');
        if ($data) {
            foreach ($data as $key => $dis) {
                $data[$key]['brand_image'] = D('Brand')->getBrandImage($dis['brand_image']);
            }
        }
        return $data;
    }

    /**
     * 获取当前关注品牌是否有品牌品牌特卖
     * @param $uid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBrandsDis($uid)
    {
        $map = array();
        $md = array();
        $fields = 'm_mall_discount.mid,m_mall_discount.fid,m_mall_discount.bid,m_mall_discount.title,m_mall_discount.itemid,m_mall_discount.start_time,m_mall_discount.end_time';
        $join = 'INNER JOIN m_brand_attention ON m_mall_discount.bid = m_brand_attention.bid';
        $map['m_mall_discount.end_time'] = array('egt', $this->getThisDayTime());
        $map['m_brand_attention.uid'] = $uid;
        $dis = $this->field($fields)->join($join)->where($map)->order('m_mall_discount.start_time DESC')->select();
        if ($dis) {
            $BrandModel = D('Brand');
            foreach ($dis as $d) {
                unset($mfb);
                $mfb = $this->getAdMFB($d['mid'], $d['fid'], $d['bid']);
                $d = array_merge($d, $mfb);
                $d['brand']['image_url'] = $BrandModel->getBrandImage($mfb['brand']['image']);
                $md[$d['bid']] = $d;
            }
        }
        return $md;
    }
}
